<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * create or update device information for a user 
 * 
 * @param args = {
 * 
 *  @param  id    
 *  @param  alias
 *  @param  brand
 *  @param  model
 *  @param  type
 *  @param  os
 *  @param  location
 *  @param  wifi
 *  @param  3G
 *  @param  user_id
 *  @param  added_ts
 *  @param  modified_ts
 *  @param  status
 *               
 * }
 */
function devices_save($args){
    
    //import required global variables 
    global $db;
    global $contex;
    
    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $id = X_param ($args, 'id'); 
    $data['alias'] = X_param ($args, 'alias'); if(!$id && !$data['alias']) return __MOOV_PARAM_INVAL_;
    $data['brand'] = X_param ($args, 'brand'); if(!$id && !$data['brand']) return __MOOV_PARAM_INVAL_;
    $data['model'] = X_param ($args, 'model'); if(!$id && !$data['model']) return __MOOV_PARAM_INVAL_;
    $data['type'] = X_param ($args, 'type'); if(!$id && !$data['type']) return __MOOV_PARAM_INVAL_;
    $data['os'] = X_param ($args, 'os'); if(!$id && !$data['os']) return __MOOV_PARAM_INVAL_;
    $data['location'] = X_param ($args, 'location'); 
    $data['wifi'] = X_param ($args, 'wifi'); 
    $data['three_G'] = X_param ($args, '3G'); 
    $data['user_id'] = X_param ($args, 'user_id'); if(!$id && !$data['user_id']) return __MOOV_PARAM_INVAL_;
    $data['added_ts'] = X_param ($args, 'added_ts', gmdate('y-m-d h:m:s')); 
    $data['modified_ts'] = X_param ($args, 'modified_ts', gmdate('y-m-d h:m:s'));
    $data['status'] = X_param ($args, 'status', 'A'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    // ---------------- Operation begins here ---------------------
    $id = $db->save('devices',$id,$data);
    return devices_list(array('id' => $id, 'fields' => $fields, 'limit' => $limit, 'sort' => $sort));    
}

/*
 * Get device information for a user 
 * 
 * @param args = {
 * 
 *  @param  id    
 *  @param  alias
 *  @param  brand
 *  @param  model
 *  @param  type
 *  @param  os
 *  @param  location
 *  @param  wifi
 *  @param  3G
 *  @param  user_id
 *  @param  added_ts
 *  @param  modified_ts
 *  @param  status
 *               
 * }
 */
function devices_list($args){
    
    //import required global variables 
    global $db;
    global $contex;

    //extract and sanitize parameters creating a variable for each
    $fields = X_param_array ($args, 'fields'); 
    $data['id'] = X_param_array ($args, 'id'); 
    $data['alias'] = X_param_array ($args, 'alias'); 
    $data['brand'] = X_param_array ($args, 'brand'); 
    $data['model'] = X_param_array ($args, 'model'); 
    $data['type'] = X_param_array ($args, 'type'); 
    $data['os'] = X_param_array ($args, 'os'); 
    $data['location'] = X_param_array ($args, 'location'); 
    $data['wifi'] = X_param_array ($args, 'wifi'); 
    $data['three_G'] = X_param_array ($args, '3G'); 
    $data['user_id'] = X_param_array ($args, 'user_id'); 
    $data['added_ts'] = X_param_array ($args, 'added_ts');
    $data['modified_ts'] = X_param_array ($args, 'modified_ts');
    $data['status'] = X_param_array ($args, 'status', 'A'); 
    $limit = X_param_array ($args, 'limit');
    $sort = X_param_array ($args, 'sort');
    
    // ---------------- Operation begins here ---------------------
    $rc = $db->get('devices', $fields, $data, $limit, $sort);
    return out($rc); 
}